
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 08/18/2014 21:32:44
-- Generated from EDMX file: G:\CTYWX\Microhard.WeixinMP\DataAccess\WXDB.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [Microhard_Plat_WX];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_MPUsers_MPAccount]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[MPUsers] DROP CONSTRAINT [FK_MPUsers_MPAccount];
GO
IF OBJECT_ID(N'[dbo].[FK_ProductImage_Conversation]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Image] DROP CONSTRAINT [FK_ProductImage_Conversation];
GO
IF OBJECT_ID(N'[dbo].[FK_SHARELIN_REFERENCE_ARTICLES]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ShareLink] DROP CONSTRAINT [FK_SHARELIN_REFERENCE_ARTICLES];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Articles]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Articles];
GO
IF OBJECT_ID(N'[dbo].[Conversation]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Conversation];
GO
IF OBJECT_ID(N'[dbo].[Feedback]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Feedback];
GO
IF OBJECT_ID(N'[dbo].[Image]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Image];
GO
IF OBJECT_ID(N'[dbo].[MPAccount]', 'U') IS NOT NULL
    DROP TABLE [dbo].[MPAccount];
GO
IF OBJECT_ID(N'[dbo].[MPInfo]', 'U') IS NOT NULL
    DROP TABLE [dbo].[MPInfo];
GO
IF OBJECT_ID(N'[dbo].[MPMessage]', 'U') IS NOT NULL
    DROP TABLE [dbo].[MPMessage];
GO
IF OBJECT_ID(N'[dbo].[MPUsers]', 'U') IS NOT NULL
    DROP TABLE [dbo].[MPUsers];
GO
IF OBJECT_ID(N'[dbo].[ShareLink]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ShareLink];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Conversations'
CREATE TABLE [dbo].[Conversations] (
    [ConversationID] uniqueidentifier  NOT NULL,
    [CreateTime] datetime  NULL,
    [WeixinName] nvarchar(50)  NULL,
    [ProcessStep] int  NULL,
    [IsFinished] bit  NULL,
    [Type] nvarchar(50)  NULL,
    [CloseTime] datetime  NULL,
    [UpdateTime] datetime  NULL
);
GO

-- Creating table 'Images'
CREATE TABLE [dbo].[Images] (
    [PicID] uniqueidentifier  NOT NULL,
    [ConversationID] uniqueidentifier  NULL,
    [ImageContent] varbinary(max)  NULL,
    [CreateTime] datetime  NULL,
    [FileType] nvarchar(max)  NULL
);
GO

-- Creating table 'MPMessages'
CREATE TABLE [dbo].[MPMessages] (
    [MPContentID] uniqueidentifier  NOT NULL,
    [MPAccountID] uniqueidentifier  NULL,
    [Content] nvarchar(max)  NULL,
    [OpenID] nvarchar(500)  NULL,
    [CreateTime] datetime  NULL,
    [IsReply] bit  NULL,
    [ReplyTime] datetime  NULL,
    [MsgId] bigint  NULL,
    [MsgType] nvarchar(50)  NULL,
    [MsgDateTime] nvarchar(200)  NULL,
    [IsCheck] bit  NULL
);
GO

-- Creating table 'MPUsers'
CREATE TABLE [dbo].[MPUsers] (
    [WXUserID] uniqueidentifier  NOT NULL,
    [FakeID] nvarchar(50)  NULL,
    [OpenID] nvarchar(500)  NULL,
    [CreateTime] datetime  NULL,
    [UpdateTime] datetime  NULL,
    [UserName] nvarchar(50)  NULL,
    [City] nvarchar(50)  NULL,
    [Province] nvarchar(50)  NULL,
    [WXMPID] uniqueidentifier  NULL,
    [BelongEntityID] uniqueidentifier  NULL
);
GO

-- Creating table 'Feedbacks'
CREATE TABLE [dbo].[Feedbacks] (
    [FeedbackID] uniqueidentifier  NOT NULL,
    [Content] nvarchar(500)  NULL,
    [CreateTime] datetime  NULL,
    [BelongEntityID] uniqueidentifier  NULL,
    [QQMail] nvarchar(50)  NULL,
    [Result] nvarchar(500)  NULL
);
GO

-- Creating table 'MPInfoes'
CREATE TABLE [dbo].[MPInfoes] (
    [InfoID] uniqueidentifier  NOT NULL,
    [Title] nvarchar(50)  NULL,
    [Content] nvarchar(max)  NULL,
    [CreateTime] datetime  NULL,
    [ClickKey] nvarchar(50)  NULL,
    [BelongEntityID] uniqueidentifier  NULL,
    [MsgType] uniqueidentifier  NULL,
    [Summary] nvarchar(100)  NULL
);
GO

-- Creating table 'MPAccounts'
CREATE TABLE [dbo].[MPAccounts] (
    [WXMPID] uniqueidentifier  NOT NULL,
    [UserName] nvarchar(50)  NULL,
    [Password] nvarchar(50)  NULL,
    [CreateTime] datetime  NULL,
    [UpdateTime] datetime  NULL,
    [AppID] nvarchar(50)  NULL,
    [AppSecret] nvarchar(50)  NULL,
    [CompanyID] uniqueidentifier  NULL,
    [CompanyName] nvarchar(50)  NULL,
    [Welcome] nvarchar(250)  NULL
);
GO

-- Creating table 'ShareLinks'
CREATE TABLE [dbo].[ShareLinks] (
    [ShareLinkID] uniqueidentifier  NOT NULL,
    [ArticleID] uniqueidentifier  NULL,
    [ShareUserOpenID] nvarchar(500)  NULL,
    [CreateTime] datetime  NULL,
    [ClickCount] int  NULL,
    [UpdateTime] datetime  NULL
);
GO

-- Creating table 'Articles'
CREATE TABLE [dbo].[Articles] (
    [ArticleID] uniqueidentifier  NOT NULL,
    [Title] nvarchar(50)  NULL,
    [ArticleContent] nvarchar(max)  NULL,
    [CreateTime] datetime  NULL,
    [CompanyID] uniqueidentifier  NULL,
    [OperatorID] uniqueidentifier  NULL,
    [Sort] int  NULL,
    [Type] nvarchar(50)  NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [ConversationID] in table 'Conversations'
ALTER TABLE [dbo].[Conversations]
ADD CONSTRAINT [PK_Conversations]
    PRIMARY KEY CLUSTERED ([ConversationID] ASC);
GO

-- Creating primary key on [PicID] in table 'Images'
ALTER TABLE [dbo].[Images]
ADD CONSTRAINT [PK_Images]
    PRIMARY KEY CLUSTERED ([PicID] ASC);
GO

-- Creating primary key on [MPContentID] in table 'MPMessages'
ALTER TABLE [dbo].[MPMessages]
ADD CONSTRAINT [PK_MPMessages]
    PRIMARY KEY CLUSTERED ([MPContentID] ASC);
GO

-- Creating primary key on [WXUserID] in table 'MPUsers'
ALTER TABLE [dbo].[MPUsers]
ADD CONSTRAINT [PK_MPUsers]
    PRIMARY KEY CLUSTERED ([WXUserID] ASC);
GO

-- Creating primary key on [FeedbackID] in table 'Feedbacks'
ALTER TABLE [dbo].[Feedbacks]
ADD CONSTRAINT [PK_Feedbacks]
    PRIMARY KEY CLUSTERED ([FeedbackID] ASC);
GO

-- Creating primary key on [InfoID] in table 'MPInfoes'
ALTER TABLE [dbo].[MPInfoes]
ADD CONSTRAINT [PK_MPInfoes]
    PRIMARY KEY CLUSTERED ([InfoID] ASC);
GO

-- Creating primary key on [WXMPID] in table 'MPAccounts'
ALTER TABLE [dbo].[MPAccounts]
ADD CONSTRAINT [PK_MPAccounts]
    PRIMARY KEY CLUSTERED ([WXMPID] ASC);
GO

-- Creating primary key on [ShareLinkID] in table 'ShareLinks'
ALTER TABLE [dbo].[ShareLinks]
ADD CONSTRAINT [PK_ShareLinks]
    PRIMARY KEY CLUSTERED ([ShareLinkID] ASC);
GO

-- Creating primary key on [ArticleID] in table 'Articles'
ALTER TABLE [dbo].[Articles]
ADD CONSTRAINT [PK_Articles]
    PRIMARY KEY CLUSTERED ([ArticleID] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [ConversationID] in table 'Images'
ALTER TABLE [dbo].[Images]
ADD CONSTRAINT [FK_ProductImage_Conversation]
    FOREIGN KEY ([ConversationID])
    REFERENCES [dbo].[Conversations]
        ([ConversationID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProductImage_Conversation'
CREATE INDEX [IX_FK_ProductImage_Conversation]
ON [dbo].[Images]
    ([ConversationID]);
GO

-- Creating foreign key on [WXMPID] in table 'MPUsers'
ALTER TABLE [dbo].[MPUsers]
ADD CONSTRAINT [FK_MPUsers_MPAccount]
    FOREIGN KEY ([WXMPID])
    REFERENCES [dbo].[MPAccounts]
        ([WXMPID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_MPUsers_MPAccount'
CREATE INDEX [IX_FK_MPUsers_MPAccount]
ON [dbo].[MPUsers]
    ([WXMPID]);
GO

-- Creating foreign key on [ArticleID] in table 'ShareLinks'
ALTER TABLE [dbo].[ShareLinks]
ADD CONSTRAINT [FK_SHARELIN_REFERENCE_ARTICLES]
    FOREIGN KEY ([ArticleID])
    REFERENCES [dbo].[Articles]
        ([ArticleID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SHARELIN_REFERENCE_ARTICLES'
CREATE INDEX [IX_FK_SHARELIN_REFERENCE_ARTICLES]
ON [dbo].[ShareLinks]
    ([ArticleID]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------